﻿using System;
using CNShapes;
using System.Drawing;
using System.Collections.Generic;
using BobcatUtility;

namespace CNPageViewer
{
    /// <summary>
    /// Renders a Shape_Text object. This holds text (possibly split over
    /// multiple lines) and specifies the font and color information.
    /// </summary>
    public class ShapeRenderer_Text : IShapeRenderer
    {
        #region IShapeRenderer Members

        /// <summary>
        /// Shows the text on the page-viewer control, scaled appropriately.
        /// </summary>
        public void renderShape(Shape shape, Graphics graphics, PageViewerControl pageViewerControl)
        {
            Shape_Text textShape = (Shape_Text)shape;

            // We find the top-left point for the text in screen cooridnates...
            PointF screenTopLeft = pageViewerControl.shapePointToClientPoint(textShape.getTopLeft());

            // We create the font and brush to write the text...
            float fFontSize = textShape.getFontSize() * pageViewerControl.ZoomFactor;
            Font font = new Font(textShape.getFontName(), fFontSize, FontStyle.Regular);
            Brush brush = new SolidBrush(textShape.getFontColor());

            // We write the lines of text...
            PointF lineLocation = Utility.copy(screenTopLeft);
            IList<string> listLines = textShape.getLines();

			int iLines = listLines.Count;
			for (int iLine = 0; iLine < iLines; ++iLine)
			{
				// We get the text for this line, and append a "cursor" if
				// it is the last one and the text is being edited...
                string strLine = listLines[iLine];
				if (iLine == iLines - 1 && textShape.getIsBeingEdited() == true)
				{
					strLine += "*";
				}

                if (strLine == "")
                {
                    // If the line is empty we draw a space, as otherwise it will not show a gap...
                    strLine = " ";
                }

                graphics.DrawString(strLine, font, brush, lineLocation);

                // We find the screen size of the line, so that we can work out
                // the starting point of the next line of text below it...
                SizeF lineSize = graphics.MeasureString(strLine, font);
                lineLocation.Y += lineSize.Height;
            }
        }

        /// <summary>
        /// Returns the type of the shape we render.
        /// </summary>
        public Type getShapeType()
        {
            return typeof(Shape_Text);
        }

        #endregion
    }
}
